# -*- encoding=utf-8 -*-
"""
simple gui tool to generate json file for template
"""

from tkinter import filedialog
from tkinter import *
import cv2
from PIL import Image, ImageTk
import os.path
import json


class StatusBar(Frame):
    def __init__(self, master):
        super().__init__(master)
        self.status = Label(self)
        self.status.pack()
    def set(self, format, *arg):
        self.status.config(text=format % arg)
    def clear(self):
        self.status.config(text='')

class TemplateManager(Frame):
    def __init__(self, master):
        super().__init__(master)
        self.pack()
        self.add_widgets()
        self.add_command()
        self.image = None
        self.image_title = ""

    def add_widgets(self):
        self.img_panel = Frame(self)
        self.img_panel.grid(row=0)
        self.img_show = Canvas(self.img_panel)
        self.img_show.pack()

        self.status_bar = StatusBar(self)
        self.status_bar.grid(row=1)
        self.control_panel = Frame(self)
        self.control_panel.grid(row=0, column=1)
        self.choose_img = Button(self.control_panel, text='选择图片')
        self.choose_img.grid(row=0)

        Label(self.control_panel, text='模板').grid(row=2)

        

        self.info_panel = Frame(self.control_panel)
        self.info_panel.grid(row=3)
        Label(self.info_panel, text='模板信息:').grid(row=0, column=0)
        Label(self.info_panel, text='宽度:').grid(row=1, column=0)
        Label(self.info_panel, text='高度:').grid(row=1, column=2)
        self.width = Entry(self.info_panel, text='', width=6)
        self.width.grid(row=1, column=1)
        self.height = Entry(self.info_panel, text='', width=6)
        self.height.grid(row=1, column=3)
        Label(self.info_panel, text='组信息:')

    def add_command(self):
        print('add command')
        self.choose_img['command'] = self.choose_image
        self.img_show.bind('<Enter>', self.get_pos)

    def get_pos(self, event):
        if event and self.image is not None:
            x,y = event.x, event.y
            self.status_bar.set('%s:(%d,%d)' % (self.image_title, x, y))

    def choose_image(self):
        print('choose img')
        filename = filedialog.askopenfile(mode='r')
        if filename:
            image_file = filename.name
            self.image_title = os.path.basename(image_file)
            self.image = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE)
            if self.image is not None:
                img = Image.fromarray(self.image)
                self.img = ImageTk.PhotoImage(img)
                self.img_show.create_image(0,0,anchor=NW, image=self.img)
                h, w = self.image.shape
                self.img_show.config(width=w, height=h)
                setEntry(self.width, w)
                setEntry(self.height, h)
                self.status_bar.set('%s: (,)', self.image_title)


def loadTemplateJson(jsonfile):
    with open(jsonfile, 'r') as jf:
        data = json.load(jf)
    return data

def setEntry(entry, s):
    assert(isinstance(entry, Entry))
    entry.delete(0, 'end')
    entry.insert(0, s)

if __name__ == '__main__':
    root = Tk()
    app = TemplateManager(root)
    app.mainloop()
